<?php 
 
namespace app\common\model;

use app\common\model\BaseModel;
use app\common\traits\ModelTrait;
use app\common\enum\BaseEnum;
use app\common\utils\JsonService;
/**
* This is the model class for table "{ptf_shop_category}".
*
* @property int $id 商品分类id 
* @property string $name 商品分类名称
* @property int $parent_id 父id 
* @property string $parent_id_path 家族图谱
* @property int $sort_order 顺序排序 
* @property int $is_show 是否显示 
*/
class ShopCategory extends BaseModel{

    protected $pk = 'id';
    protected $name = 'shop_category';

    use ModelTrait;
    protected $autoWriteTimestamp = true;
    //protected $dateFormat = 'Y-m-d';
    //protected $createTime = 'create_time';
    //protected $updateTime = 'update_time';

    /**
    * {@inheritdoc}
    */
    public function attributeLabels()
    {
        return [
            'id' => '商品分类id',
            'name' => '商品分类名称',
            'parent_id' => '父id',
            'parent_id_path' => '家族图谱',
            'sort_order' => '顺序排序',
            'is_show' => '是否显示',
        ];
    }

    /*
     *获取分类列表
     * @return array;
    */
    public static function getTree($parent_id = 0,$where=[]){
        $arr = self::getAll($where,'sort_order desc')->each(function($item){
            $item['is_show_status'] = BaseEnum::getBoolValue($item['is_show']);
        })->toArray();
        return self::_sort($arr,$parent_id);
    }


    /*
     * 递归排序获取所有子栏目
     * $param array $arr
     * $param $parent_id int
     * $param $lev int
     * $param $isClear boolean
     * */
    public static function _sort($arr,$parent_id=0,$lev=1,$isClear=true){
        static $tree = [];
        if($isClear==true){
            $tree = [];
        }

        foreach($arr as $k=>$v) {
            if ($v['parent_id'] == $parent_id) {
                $v['lev'] = $lev;
                $tree[] = $v;
                self::_sort($arr,$v['id'],$lev+1,false);
            }
        }
        return $tree;
    }


    /*
     * 获取上级ID递归排序
    */
    public static function _family($arr,$cat_id=0,$isClear=true){
        static $data = array();
        if($isClear){
            $data = array();
        }
        foreach($arr as $k=>$v){
            if($v['id'] == $cat_id){
                self::_family($arr,$v['parent_id'],false);
                $data[] = $v;
            }
        }
        return $data;
    }

    /*
     * 根据当前ID获取所有上级
    */
    public static function getFamily($cat_id){
        $arr = self::getAll([],'sort_order desc')->toArray();
        return self::_family($arr,$cat_id);
    }


    /*
     * 获取菜单
     *@param mix
     *@return array
     * */
    public static function getCategory($arr,$parent_id=0,$lev=1){
        //取出所有的权限并递归排序
        $tree = [];
        foreach($arr as $k=>$v) {
            if ($v['parent_id'] == $parent_id) {
                $v['lev'] = $lev;
                $v['children'] = self::getCategory($arr,$v['id'],$lev+1);
                $tree[] = $v;
            }
        }
        return $tree;
    }

    /*
     * 获取首页分类
     * */
    public static function getAllCategory($shop_id = 0){
        $arr = self::getAll(['is_show'=>1,'shop_id'=>$shop_id])->toArray();
        $data = self::getCategory($arr);
        return $data;
    }

}


?>